<template>
    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body @close="close">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="配置名称" prop="configName">
                <el-input v-model="form.configName" placeholder="请输入配置名称"/>
            </el-form-item>
            <el-form-item label="表名" prop="tableName">
                <el-input v-model="form.tableName" placeholder="请输入表名"/>
            </el-form-item>
            <el-form-item label="字段" prop="field">
                <el-input v-model="form.field" placeholder="请输入字段"/>
            </el-form-item>
            <el-form-item label="单位" prop="fieldUnit">
                <el-input v-model="form.fieldUnit" placeholder="请输入单位"/>
            </el-form-item>
            <el-form-item label="阈值类型" prop="thresholdType">
                <el-select v-model="form.thresholdType" placeholder="请选择阈值类型" clearable>
                    <el-option v-for="item in thresholdTypeOptions"
                               :key="item.dictValue"
                               :label="item.dictLabel"
                               :value="item.dictValue"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="运算符" prop="symbols">
                <el-select v-model="form.symbols" placeholder="请选择运算符" clearable>
                    <el-option v-for="item in symbolsOptions"
                               :key="item.dictValue"
                               :label="item.dictLabel"
                               :value="item.dictValue"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="通知阈值数" prop="notifyThreshold">
                <el-input v-model="form.notifyThreshold" placeholder="请输入通知阈值数"/>
            </el-form-item>
            <el-form-item label="警告阈值数" prop="warningThreshold">
                <el-input v-model="form.warningThreshold" placeholder="请输入警告阈值数"/>
            </el-form-item>
            <el-form-item label="紧急阈值数" prop="emergencyThreshold">
                <el-input v-model="form.emergencyThreshold" placeholder="请输入紧急阈值数"/>
            </el-form-item>
            <el-form-item label="告警策略" prop="strategyId">
                <template v-slot="scope">
                    <el-select filterable :filter-method="filterStrategy"
                               v-model="form.strategyIds" :multiple="true">
                        <el-option
                                v-for="strategy in strategyListData"
                                :key="strategy.strategyId"
                                :label="strategy.strategyName"
                                :value="strategy.strategyId"
                        ></el-option>
                    </el-select>
                </template>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import {getStrategyList} from '/src/api/alarm/strategy'
    import {updateConfig, addConfig} from '/src/api/alarm/configure'

    export default {
        name: "update",
        emits: ['dialogStatusChanged'],
        props: {
            open: Boolean,
            title: String,
            form: Object,
            thresholdTypeOptions: Array,
            symbolsOptions: Array,
        },
        data() {
            return {
                // 表单校验
                rules: {
                    configName: [
                        {required: true, message: "配置名称不能为空", trigger: "blur"}
                    ],
                    tableName: [
                        {required: true, message: "表名不能为空", trigger: "blur"}
                    ],
                    field: [
                        {required: true, message: "字段不能为空", trigger: "blur"}
                    ],
                    fieldUnit: [
                        {required: true, message: "单位不能为空", trigger: "blur"}
                    ],
                    thresholdType: [
                        {required: true, message: "阈值类型不能为空", trigger: "blur"}
                    ],
                    symbols: [
                        {required: true, message: "运算符不能为空", trigger: "blur"}
                    ],
                    notifyThreshold: [
                        {required: true, message: "通知阈值数不能为空", trigger: "blur"}
                    ],
                    warningThreshold: [
                        {required: true, message: "警告阈值数不能为空", trigger: "blur"}
                    ],
                    emergencyThreshold: [
                        {required: true, message: "紧急阈值数不能为空", trigger: "blur"}
                    ],
                    strategyId: [
                        {required: true, message: "告警策略不能为空", trigger: "blur"}
                    ],
                },
                queryParams: {
                    strategyName: undefined
                },
                //告警策略列表
                strategyListData: [],
            }
        },
        created() {
            this.getStrategyListByParam()
        },
        methods: {
            //提交
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        if (this.form.configId !== undefined) {
                            updateConfig(this.form).then(response => {
                                this.msgSuccess('修改成功')
                                this.close()
                            })
                        } else {
                            addConfig(this.form).then(response => {
                                this.msgSuccess('新增成功')
                                this.close()
                            })
                        }
                    }
                })
            },
            //取消
            cancel() {
                this.$emit('dialogStatusChanged', false)
            },
            //关闭
            close() {
                this.$emit('dialogStatusChanged', false)
            },
            //获取策略列表
            getStrategyListByParam() {
                getStrategyList(this.queryParams).then((res) => {
                    this.strategyListData = res.rows
                })
            },
            filterStrategy(strategyName) {
                this.queryParams.strategyName = strategyName
                this.getUserListByParam()
            },
        }
    }
</script>

<style scoped>

</style>
